package com.yeyks.controller.user;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.constants.activity.RedisKeyPreConstant;
import com.yeyks.common.em.accountinfo.AccountInfoTypeEnum;
import com.yeyks.common.result.user.UserDataStatInviteSortResult;
import com.yeyks.common.result.user.UserDataStatTotalResult;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.commonReference.append.activity.ActivityZhouJieLunService;
import com.yeyks.consumer.dal.domain.UserDataStat;
import com.yeyks.consumer.service.UserDataStatService;
import com.yeyks.consumer.service.UserInviteRelationService;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户数据统计 前端控制器
 * </p>
 *
 * @author shuai
 * @since 2019-11-07
 */
@Api(tags = "用户数据统计")
@Slf4j
@RestController
@RequestMapping("/userDataStat")
public class UserDataStatController {
    @Autowired
    private UserDataStatService userDataStatService;
    @Autowired
    private UserInviteRelationService userInviteRelationService;
    @Autowired
    private ActivityZhouJieLunService activityZhouJieLunService;

    @ApiOperation(value = "用户数据统计 --> 总结果", notes = "总结果", response = UserDataStatTotalResult.class)
    @PostMapping(value = "/total/result")
    @NeedToken
    public AjaxResponseBody<UserDataStatTotalResult> totalResult() {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        Arguments.isTrue(!AccountInfoTypeEnum.Administrator.value().equals(loginUser.getType()), "您无权访问");
        UserDataStatTotalResult result = userDataStatService.totalResult();
        UserDataStat activityData = activityZhouJieLunService.getRedisActivityData(RedisKeyPreConstant.STATISTICAL_DATA_PRE + RedisKeyPreConstant.STATISTICAL_DATA_MIDDLE_ACTIVITY + RedisKeyPreConstant.STATISTICAL_DATA_MIDDLE_ACTIVITY_END_ZHOUJIELUN_COUNT);
        if (null == result) {
            result = ObjectConvertUtil.convertObject(activityData, UserDataStatTotalResult.class);
            return AjaxResponseBody.success(result);
        }
        if (null != activityData) {
            result.setActivityInCount(result.getActivityInCount() + activityData.getActivityInCount());
            result.setActivityShareCount(result.getActivityShareCount() + activityData.getActivityShareCount());
            result.setActivityVisitCount(result.getActivityVisitCount() + activityData.getActivityVisitCount());
            result.setNewUserCount(result.getNewUserCount() + activityData.getNewUserCount());
        }
        return AjaxResponseBody.success(result);
    }

//    @ApiOperation(value = "用户数据统计 --> 邀请人数排名", notes = "邀请人数排名", response = UserDataStatInviteSortResult.class)
//    @ApiImplicitParam(paramType = "query", name = "type", value = "type默认3(1:0元邀请 2:创客邀请 3:周杰伦活动用户)", dataType = "int")
//    @PostMapping(value = "/invite/sort")
//    @NeedToken
//    public AjaxResponseBody<List<UserDataStatInviteSortResult>> inviteSort(@RequestParam(name = "type", defaultValue = "3") Integer type) {
//        AccountInfo loginUser = AccountUtils.getLoginUser();
//        Arguments.isTrue(!AccountInfoTypeEnum.Administrator.value().equals(loginUser.getType()), "您无权访问");
//        List<UserDataStatInviteSortResult> result = userInviteRelationService.inviteSort(type);
//        return AjaxResponseBody.success(result);
//    }

    @ApiOperation(value = "用户数据统计 --> 邀请人数排名", notes = "邀请人数排名", response = UserDataStatInviteSortResult.class)
    @PostMapping(value = "/invite/sort")
    @NeedToken
    public AjaxResponseBody<List<UserDataStatInviteSortResult>> inviteSort() {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        Arguments.isTrue(!AccountInfoTypeEnum.Administrator.value().equals(loginUser.getType()), "您无权访问");
        List<UserDataStatInviteSortResult> result = activityZhouJieLunService.inviteSort();
        return AjaxResponseBody.success(result);
    }

}
